//
//  HZWelComeViewController.swift
//  HansWeibo
//
//  Created by Hans on 16/8/12.
//  Copyright © 2016年 Hans. All rights reserved.
//
import SDWebImage
import UIKit

class HZWelComeViewController: UIViewController {

    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 更改头像的约束
        self.headImageView.snp_updateConstraints { (make) in
            make.top.equalTo(self.view).offset(100)
        }

        /*
            动画时间
            延迟时间
            阻尼系数 (0-1) 越小弹性越大
            起始速度
            options
         */
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            //强制 刷新ui
            self.view.layoutIfNeeded()
            
            }) { (_) in
                UIView.animateWithDuration(0.25, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        // 延时一秒 切换
                        let time: NSTimeInterval = 1.0
                        let d = dispatch_time(DISPATCH_TIME_NOW, Int64 (time * Double(NSEC_PER_SEC)))
                        dispatch_after(d, dispatch_get_main_queue(), {
                            
                            // 发送通知 告知appdelegate 切换根控制器
                            NSNotificationCenter.defaultCenter().postNotificationName(NSNOTIFICATIONCENTER, object: nil)
                        })
                })
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    // MARK: - 设置视图
    private func setupUI(){
        //添加控件
        view.addSubview(bgImageView)
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 设置约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    // 懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView(imageName: "ad_background")
    private lazy var headImageView: UIImageView = {
        let image = UIImageView(imageName: "avatar_default_big")
        
        image.HZsetImageWithString(HZUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeholderImageName: "avatar_default_big")
        
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        return image
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(18)
        label.text = "欢迎回来\(HZUserAccountViewModel.sharedTools.userAccount?.screen_name ?? "")"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.alpha = 0
        return label
    }()
    
}
